﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Stuff
{
    class StuffFileEntryStream : Stream
    {

        public StuffFileEntryStream(StuffFileEntry stuffFile)
            {
                this.stuffFile = stuffFile;
                fileStream = File.OpenRead(stuffFile.StuffFullName);
                fileStream.Seek(stuffFile.Offset, SeekOrigin.Begin);
            }

            public override void Close()
            {
                fileStream.Close();
                base.Close();
            }

            StuffFileEntry stuffFile = null;
            FileStream fileStream = null;

            public override bool CanRead
            {
                get { return true; }
            }

            public override bool CanSeek
            {
                get { return true; }
            }

            public override bool CanWrite
            {
                get { return false; }
            }

            public override void Flush()
            {
                throw new Exception("The method or operation is not implemented.");
            }

            public override long Length
            {
                get { return stuffFile.Length; }
            }

            public override long Position
            {
                get
                {
                    return fileStream.Position - stuffFile.Offset;
                }
                set
                {
                    fileStream.Position = value + stuffFile.Offset;
                }
            }

            public override int Read(byte[] buffer, int offset, int count)
            {
                if (Position + count > stuffFile.Length)
                    count = stuffFile.Length - (int)Position;
                return fileStream.Read(buffer, offset, count);
            }

            public override long Seek(long offset, SeekOrigin origin)
            {
                switch (origin)
                {
                    case SeekOrigin.Begin:
                        if (offset <= stuffFile.Length)
                        {
                            fileStream.Seek(stuffFile.Offset + offset, SeekOrigin.Begin);
                            return Position;
                        }
                        break;
                    case SeekOrigin.Current:
                        if (Position + offset <= (long)stuffFile.Length)
                        {
                            fileStream.Seek(offset, SeekOrigin.Current);
                            return Position;
                        }
                        break;
                    case SeekOrigin.End:
                        if (offset <= stuffFile.Length)
                        {
                            fileStream.Seek(stuffFile.Offset + ((long)stuffFile.Length) - offset, SeekOrigin.Begin);
                            return Position;
                        }
                        break;
                }
                throw new System.IO.IOException();
            }

            public override void SetLength(long value)
            {
                throw new Exception("The method or operation is not implemented.");
            }

            public override void Write(byte[] buffer, int offset, int count)
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }
}
